<?php
/**
 * 页面管理 - JBlog
 * 
 * @copyright (c) 2008-2010 JBlog (www.lisijie.org)
 * @author lisijie <lisijie86@gmail.com>
 * @version $Id: mod_page.php 523 2010-06-23 10:06:24Z lisijie86 $
*/

!defined('IN_ADMIN') && exit('Access Denied!');

if ( !is_founder() ) { prompt(__('对不起，你没有权限执行这项操作。')); }

require_once JBLOG_INC.'func_post.php';

empty($ac) && $ac = 'list';

if ( $ac == 'list' ) {
	
	$page = max(1, intval($_GET['page']));
	$pagesize = 20;
	$offset = ($page - 1) * $pagesize;
	$count = $db->count('post', "`type` = 'page'");
	$url = '?mod=page';
	$result = array();
	if ( $count ) {
		$result = $db->fetch_all("SELECT p.id,p.title,p.userid,p.username,p.views,p.comments,p.dateline,p.status,p.password,p2.id AS parent_id,p2.title AS parent_title
		FROM ".tname('post')." p LEFT JOIN ".tname('post')." p2 ON p.parentid = p2.id WHERE p.`type` = 'page' ORDER BY p.id DESC LIMIT $offset, $pagesize");
	}
	include admin_tpl('page');

} elseif ( $ac == 'create' ) {
	
	initGP('id','GP','int');
	
	get_cache('pagemeta');
	
	if ( check_submit() ) {
		if ( empty($_POST['title']) ) {
			$_POST['title'] = __('未命名文章');
		}
		$old = array();
		if ( $id ) {
			$old = get_post($id);
		}
		initGP(array('year','month','day','hour','minute','second'),'P','int');
		$posttime = mktime($hour, $minute, $second, $month, $day, $year);
		if ( !$posttime ) $posttime = NOW;
		$_POST['dateline'] = $posttime;
		$id = post_post($_POST, $old); //发布内容
		//检查文章附件
		$attachments = get_cookie('upload_attachments');
		if ( !empty($attachments) && preg_match('/^([0-9]+(,[0-9])?)+$/', $attachments) ) {
			$db->query("UPDATE ".tname('attach')." SET postid = $id WHERE id IN ($attachments)");
			set_cookie('upload_attachments');
		}
		do_action('page_create_submit');
		redirect('?mod=page&ac=list');
	} else {
		$result = array('commentstatus'=>1);
		//热门标签
		$hottags = '';
		$query = $db->query("SELECT * FROM ".tname('tag')." ORDER BY `count` LIMIT 50");
		while ( $row = $db->fetch_array($query) ) {
			$hottags .= '<a href="javascript:;">'.$row['tagname'].'</a>';
		}
		//编辑
		if ( $id ) {
			$result = get_post($id);
			foreach ($result as $key => $val) {
				$result[$key] = my_htmlspecialchars($val);
			}
		}
		$datetime = getdate($result['dateline']?$result['dateline']:NOW);
	}
	
	include admin_tpl('pagecreate');

//删除文章
} elseif ( $ac == 'delete' ) {
	
	$ids = $_POST['id'];
	
	if ( empty($ids) ) $ids = array(intval($_GET['id']));
	del_post($ids);
	do_action('page_delete', $ids);
	redirect('?mod=page');
	
//修改状态
} elseif ( $ac == 'upstat') {
	
	$ids = $_POST['id'];
	$status = intval($_POST['status']);
	foreach ( (array)$ids as $key => $val ) {
		if ( !$val || !is_numeric($val) ) unset($ids[$key]);
	}
	if ( $ids ) {
		$db->update('post', array('status'=>$status), array('id'=>$ids));
	}
	do_action('page_upstat', $ids);
	redirect($_USER['refer']);

//--------自定义域------
} elseif ( $ac == 'pagemeta' ) {
	
	empty($do) && $do = 'list';
	
	//保存
	if ( $do == 'save' ) {
		
		$name = trim($_POST['name']);
		if ( !preg_match("/^[\x{4e00}-\x{9fa5}A-Za-z0-9_]+$/u", $name) ) {
			prompt(__('自定义域名称必须由汉字、字母、数字、下划线构成。'));
		}
		$query = $db->query("DESC ".tname('post')."");
		while ( $row = $db->fetch_array($query) ) {
			$fields[] = $row['Field'];
		}
		if ( in_array($name, $fields) ) {
			prompt(__('名称与系统字段冲突，请重新填写。'));
		}
		if ( $db->result("SELECT `name` FROM ".tname('config')." WHERE `name` = '$name' AND `type` != 'pagemeta'") ) {
			prompt(__('该配置项已存在，请换一个名称。'));
		}
		$value = (array)$_POST['value'];
		$data = array(
			'name' => $name,
			'value' => my_serialize($value),
			'type' => 'pagemeta'
		);
		$db->insert('config', $data, true);
		do_action('page_pagemeta_save');
		recache('pagemeta');
		redirect('admin.php?mod=page&ac=pagemeta');
		
	//编辑
	} elseif ( $do == 'edit' ) {
		
		$name = trim($_GET['name']);
		$meta = $db->fetch_one_array("SELECT * FROM ".tname('config')." WHERE `name` = '$name' AND `type` = 'pagemeta'");
		if ( $meta ) {
			$meta['value'] = unserialize($meta['value']);
			$meta['value']['name'] = $meta['name'];
			$meta = $meta['value'];
		}
		
	//删除
	} elseif ( $do == 'del' ) {
		
		$name = trim($_GET['name']);
		$db->delete('config', array('name'=>$name, 'type'=>'pagemeta'));
		recache('pagemeta');
		redirect('admin.php?mod=blog&ac=blogmeta');
		
	} elseif ( $do == 'list' ) {
	
		$result = array();
		$query = $db->query("SELECT `name`,`value` FROM ".tname('config')." WHERE `type` = 'pagemeta'");
		while ( $row = $db->fetch_array($query) ) {
			$row['value'] = unserialize($row['value']);
			$row['value']['name'] = $row['name'];
			$result[] = $row['value'];
		}
	
	}
	
	include admin_tpl('pagemeta');
	
}

//父级页面下拉菜单
function parent_page_options($selid=0) {
	global $db;
	$output = '';
	$query = $db->query("SELECT id,title FROM ".tname('post')." WHERE `type` = 'page' AND parentid = '0'");
	while ( $row = $db->fetch_array($query) ) {
		$output .= '<option value="'.$row['id'].'"'.($selid==$row['id']?' selected':'').'>'.$row['title'].'</option>';
	}
	return $output;
}
?>